﻿@using NewLife;
@using NewLife.Web;
@using XCode;
@{
    var fact = ViewBag.Factory as IEntityFactory;
    var page = ViewBag.Page as Pager;

    var act = Context.Request.Path + "";
    if (act.IsNullOrEmpty()) { act = Url.Action("Index"); }

    var set = ViewBag.PageSetting as PageSetting ?? PageSetting.Global;
}
<div class="col-12">
    <form action="@Html.Raw(page.GetFormAction(act))" method="post" role="form">
        <div class="row">
            @if (set.EnableAdd && !set.IsReadOnly && this.Has(PermissionFlags.Insert))
            {
                var rv = page.GetRouteValue();
                <div class="col">
                    @*@Html.ActionLink("添加", "Add", rv, new { @class = "btn btn-primary" }.ToDictionary())*@
                    <a href="@Url.Action("Add",rv)" class="btn btn-primary">
                        <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none" /><line x1="12" y1="5" x2="12" y2="19" /><line x1="5" y1="12" x2="19" y2="12" /></svg>
                        添加
                    </a>
                </div>
            }
            @if (set.EnableSelect)
            {
                <div class="col toolbar-batch">
                    @await Html.PartialAsync("_List_Toolbar_Batch")
                </div>
            }
            @await Html.PartialAsync("_List_Toolbar_Custom")
            <div class="col-auto">
                <div class="d-flex">
                    @await Html.PartialAsync("_List_Search")
                </div>
            </div>
            @await Html.PartialAsync("_List_Toolbar_Search")
            <div class="col">
                @await Html.PartialAsync("_List_Toolbar_Adv")
            </div>
        </div>
    </form>
</div>
@if (set.EnableSelect)
{
    <script>
        $(function () {
            var $toolbarContext = $('.toolbar-batch'),
                $batchButtons = $('button[data-action="action"], input[data-action="action"]'), //button, input=button, a 3种类型都可以
                $table = $('.table'),
                $keys = $('input[name="keys"]', $table);

            $table.on('click', '#chkAll', function () {
                // 全选
                $keys.prop('checked', this.checked);
                // 启用禁用批量操作区
                $batchButtons.prop('disabled', !this.checked);
            });

            $table.on('click.checked', 'tbody input[name="keys"]', function (e) {
                //页面中所有的checkbox
                var allbox = $table.find('tbody :checkbox[name="keys"]');
                //页面中所选中的checkbox
                var selecteds = $table.find('tbody :checkbox:checked[name="keys"]');
                if (selecteds.length > 0) {
                    // 启用禁用批量操作区
                    $batchButtons.prop('disabled', false);
                    //需要判断当前页面所有行的checkbox是否都选中，以此来决定是否需要改变checkbox#chkAll 的状态
                    if (allbox.length == selecteds.length) {
                        $table.find('#chkAll').prop('checked', true);
                    } else {
                        $table.find('#chkAll').prop('checked', false);
                    }
                }
                else {
                    $batchButtons.prop('disabled', true);
                    $table.find('#chkAll').prop('checked', false);
                }
            });
        })
    </script>
}
